/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial');

GBIF.GeoSpatial.WFSCapabilitiesLayers = function(config){
	
	this.geoMapPanel = new GBIF.GeoSpatial.MapComponent({
			region : 'center'
		,	border: false
	});
	
	this.geoMapPanel.setTitle(this.txtgeoMapPanelTitle);
	
	this.geoGridPanel=new GBIF.GeoSpatial.WFSCapabilitiesGrid ({	
			region : 'west'
		,	split: true
		,	autoScroll: true 
		,	border: false
	});
	
	this.geoGridPanel.on('addLayer', this.loadLayer, this);
	this.geoGridPanel.on('previewLayers', this.showLayers, this);
	
	Ext.apply(this,config,{
			layout: 'border'
		,	border: false
		,	tbar: [{
					xtype:'label'
				,	style:"padding:0px 5px"	
				,	text:this.txtWFSUrl
			},{
					xtype: 'textfield'  
				,	width:350
				,	ref: '../url'
				,	vtype:'url'
				, enableKeyEvents: true 
				, listeners: {
						keypress:{
								scope:this
							,	fn: function( textfield, e ) {
									if (e.getKey() == e.ENTER) {
										this.loadUrl();
									}
								}
						}
					}	 
			}, {
					text: this.txtLoad
				,	style:"padding:0px 5px"
				,	handler: this.loadUrl
				, scope: this
			}]
		,	proxyUrl: "../resources/api/getcapabilities-proxy.php?url="
		,	items: [
					this.geoMapPanel
				,	this.geoGridPanel
			]
	});
	
	GBIF.GeoSpatial.WFSCapabilitiesLayers.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.GeoSpatial.WFSCapabilitiesLayers,Ext.Panel, {

		loadUrl: function(){
			this.url.setValue(this.url.getValue().trim());
			if (this.url.isValid()) {
				var url = this.url.getValue();
				if (url != '') {
					var url = this.confirmURL(url);
					this.geoGridPanel.store.baseParams = {
						url: url
					};
					this.geoGridPanel.store.load();
				}	else {
					GBIF.GeoSpatial.Notice.msg(this.txtError, this.txtUrlError);
				}
			} else {
				GBIF.GeoSpatial.Notice.msg(this.txtError,"Please enter valid URL.");	
			}		
		}
	
	,	loadLayer:function(layer){
			this.wfslayerdata=[];
			var selecteddata = this.geoGridPanel.getSelectionModel().getSelections();
			Ext.each(selecteddata,function(item){
				this.wfslayerdata.push(item)	
			},this);
		}
	
	,	confirmURL:function(url){
			var present=false;	
			for (var i = 0; i < url.length; i++) {
				if (url.charAt(i)== "?") {
					present = true;
				}
			}
			if (present) {
				return url;
			} else {
				for (var j = 0; j < url.length; j++) {
					var url1 = url.split("&")[0];
					var url2 = url1.split("service")[0];
					var url3 = url2.split("request")[0];
					var url4 = url3.split("version")[0];
					var url5 = url.split(url4)[1];
//					console.log(url4+"?"+url5);
					return url4+"?"+url5;
				}
			}				
		}
	
	,	showLayers: function(layerWin){
			this.clearLayer();
			Ext.each(layerWin, function(maplayer){
				var layer = maplayer.data.layer.clone();
				this.geoMapPanel.map.addLayer(layer);
			}, this);
		}	
		
	,	clearPanel:function(){
			this.url.setValue('');	
			this.geoGridPanel.store.removeAll();
		}
		
	,	clearLayer:function(){
			Ext.each(this.geoMapPanel.map.layers,function(maplayer){
				var layerPresent = (typeof maplayer == 'undefined') ? false : true;
				if (layerPresent) {
					if (!maplayer.isBaseLayer) {
						this.geoMapPanel.map.removeLayer(maplayer);
					}
				}
			},this);
		}	

});